{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5b1036f1-6cec-405a-b227-b041dbcfa335",
   "metadata": {},
   "source": [
    "# 线性代数（numpy.linalg）\n",
    "\n",
    "NumPy线性代数函数依赖于BLAS和LAPACK来提供标准线性代数算法的高效低级实现。 这些库可以由NumPy本身使用其参考实现子集的C版本提供， 但如果可能，最好是利用专用处理器功能的高度优化的库。 这样的库的例子是OpenBLAS、MKL(TM)和ATLAS。因为这些库是多线程和处理器相关的， 所以可能需要环境变量和外部包（如threadpoolctl）来控制线程数量或指定处理器体系结构。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "93624886-091c-4169-8fdf-5875eda1f8c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc086c5d-21db-4633-9a44-00e9a4bd3fce",
   "metadata": {
    "tags": []
   },
   "source": [
    "# 矩阵和向量积\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "dot(a, b[, out])|两个数组的点积。\n",
    "linalg.multi_dot(arrays)|在单个函数调用中计算两个或更多数组的点积，同时自动选择最快的求值顺序。\n",
    "vdot(a, b)|返回两个向量的点积。\n",
    "inner(a, b)|两个数组的内积。\n",
    "outer(a, b[, out])|计算两个向量的外积。\n",
    "matmul(x1, x2, /[, out, casting, order, …])|两个数组的矩阵乘积。\n",
    "tensordot(a, b[, axes])|沿指定轴计算张量点积。\n",
    "einsum(subscripts, *operands[, out, dtype, …])|计算操作数上的爱因斯坦求和约定。\n",
    "einsum_path(subscripts, *operands[, optimize])|通过考虑中间数组的创建，计算einsum表达式的最低成本压缩顺序。\n",
    "linalg.matrix_power(a, n)|将方阵提升为(整数)n次方。\n",
    "kron(a, b)|两个数组的Kronecker乘积。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e51d7de-d9fc-45c4-aef6-2dbc135efb72",
   "metadata": {},
   "source": [
    "## numpy.dot(a, b, out=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f7566396-ed75-4fbf-b084-27a1a1f7b53d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(3, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "54afacb4-b342-4fcd-8be1-be83616c2317",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(-13+0j)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot([2j, 3j], [2j, 3j])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "837049e0-8892-4c36-869a-b6b0b81441e6",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 1],\n",
       "       [2, 2]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [[1, 0], [0, 1]]\n",
    "b = [[4, 1], [2, 2]]\n",
    "np.dot(a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "770ac76b-303f-41d3-9392-28a3d9260d9a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "499128"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(3*4*5*6).reshape((3,4,5,6))\n",
    "b = np.arange(3*4*5*6)[::-1].reshape((5,4,6,3))\n",
    "np.dot(a, b)[2,3,2,1,2,2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5a9bed2c-a532-473a-9f15-9a0a0d51d0a2",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "499128"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(a[2,3,2,:] * b[1,2,:,2])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0890317e-9754-426a-8f03-813fdd87c51e",
   "metadata": {},
   "source": [
    "## linalg.multi_dot(arrays, *, out=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c691cce9-d422-48c6-9e4c-bd5a9e627da6",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from numpy.linalg import multi_dot\n",
    "A = np.random.random((10000, 100))\n",
    "B = np.random.random((100, 1000))\n",
    "C = np.random.random((1000, 5))\n",
    "D = np.random.random((5, 333))\n",
    "_ = multi_dot([A, B, C, D])\n",
    "_ = np.dot(np.dot(np.dot(A, B), C), D)\n",
    "_ = A.dot(B).dot(C).dot(D)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ffac7854-eb35-4c44-8127-2caf90fc33e9",
   "metadata": {},
   "source": [
    "## numpy.vdot(a, b, /)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "57e585fb-87f1-4fd6-ab79-9f91cb2b8a0e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(70-8j)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([1+2j,3+4j])\n",
    "b = np.array([5+6j,7+8j])\n",
    "np.vdot(a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "81f4f085-8f25-4e3c-93db-933d115ca32a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(70+8j)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vdot(b, a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "dee78cda-ddf4-4ec1-8547-c53cc25b162e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "30"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1, 4], [5, 6]])\n",
    "b = np.array([[4, 1], [2, 2]])\n",
    "np.vdot(a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "da277c50-6096-4d57-a783-e0806374e1e3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "30"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vdot(b, a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "dc121025-ccbe-498d-9d90-76e1d1b5aef4",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "30"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1*4 + 4*1 + 5*2 + 6*2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46f3be4d-91db-4114-8055-89dffb7a89c0",
   "metadata": {},
   "source": [
    "## numpy.inner(a, b, /)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "ffab6eee-f3c2-4cf8-9b6c-ef59d81b8f3d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([1,2,3])\n",
    "b = np.array([0,1,0])\n",
    "np.inner(a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "fe925f3c-dd7e-4e3d-9bbe-75adcfcf3d00",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((2, 3),\n",
       " array([[ 14,  38,  62],\n",
       "        [ 86, 110, 134]]))"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(24).reshape((2,3,4))\n",
    "b = np.arange(4)\n",
    "c = np.inner(a, b)\n",
    "c.shape, c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "8945af7c-7126-4bfa-a4d9-55214cb13fc7",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((1, 1, 3), array([[[1, 3, 5]]]))"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(2).reshape((1,1,2))\n",
    "b = np.arange(6).reshape((3,2))\n",
    "c = np.inner(a, b)\n",
    "c.shape, c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "45a76cfc-c78a-43a0-9311-887bcd967712",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[7., 0.],\n",
       "       [0., 7.]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.inner(np.eye(2), 7)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64bd3d8f-a46f-4bd2-bf2e-32f99793bef8",
   "metadata": {},
   "source": [
    "## numpy.outer(a, b, out=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "6e204375-6563-4f52-9aad-ef11888d570f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-2., -1.,  0.,  1.,  2.],\n",
       "       [-2., -1.,  0.,  1.,  2.],\n",
       "       [-2., -1.,  0.,  1.,  2.],\n",
       "       [-2., -1.,  0.,  1.,  2.],\n",
       "       [-2., -1.,  0.,  1.,  2.]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rl = np.outer(np.ones((5,)), np.linspace(-2, 2, 5))\n",
    "rl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "8916aed8-f370-4675-9565-4258ed7a92fb",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.+2.j, 0.+2.j, 0.+2.j, 0.+2.j, 0.+2.j],\n",
       "       [0.+1.j, 0.+1.j, 0.+1.j, 0.+1.j, 0.+1.j],\n",
       "       [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
       "       [0.-1.j, 0.-1.j, 0.-1.j, 0.-1.j, 0.-1.j],\n",
       "       [0.-2.j, 0.-2.j, 0.-2.j, 0.-2.j, 0.-2.j]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "im = np.outer(1j*np.linspace(2, -2, 5), np.ones((5,)))\n",
    "im"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "a466d7d4-f5b5-4040-992a-df0d00032f27",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-2.+2.j, -1.+2.j,  0.+2.j,  1.+2.j,  2.+2.j],\n",
       "       [-2.+1.j, -1.+1.j,  0.+1.j,  1.+1.j,  2.+1.j],\n",
       "       [-2.+0.j, -1.+0.j,  0.+0.j,  1.+0.j,  2.+0.j],\n",
       "       [-2.-1.j, -1.-1.j,  0.-1.j,  1.-1.j,  2.-1.j],\n",
       "       [-2.-2.j, -1.-2.j,  0.-2.j,  1.-2.j,  2.-2.j]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grid = rl + im\n",
    "grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "05dc1e3e-93e2-48b9-abde-6009596c11d9",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([['a', 'aa', 'aaa'],\n",
       "       ['b', 'bb', 'bbb'],\n",
       "       ['c', 'cc', 'ccc']], dtype=object)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array(['a', 'b', 'c'], dtype=object)\n",
    "np.outer(x, [1, 2, 3])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da545551-f62e-4cb6-9d7a-a41a7e175033",
   "metadata": {},
   "source": [
    "## numpy.matmul(x1, x2, /, out=None, *, casting='same_kind', order='K', dtype=None, subok=True[, signature, axes, axis]) = <ufunc 'matmul'>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "19717518-474e-49bf-b865-8db721e22837",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 1],\n",
       "       [2, 2]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1, 0],\n",
    "              [0, 1]])\n",
    "b = np.array([[4, 1],\n",
    "              [2, 2]])\n",
    "np.matmul(a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "aed08b2e-9bc0-432b-9f04-a8498257dc4d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1, 0],\n",
    "              [0, 1]])\n",
    "b = np.array([1, 2])\n",
    "np.matmul(a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "99218256-7736-4871-9903-6ea6e85d94ee",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.matmul(b, a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "0748245a-5b6b-4d95-84bb-928aff657329",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 2, 2)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(2 * 2 * 4).reshape((2, 2, 4))\n",
    "b = np.arange(2 * 2 * 4).reshape((2, 4, 2))\n",
    "np.matmul(a,b).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "d6251e99-e987-4a21-af49-9003e65e01be",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "98"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.matmul(a, b)[0, 1, 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "3ad44315-b885-4741-a7ce-5a7e32fd2263",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "98"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(a[0, 1, :] * b[0 , :, 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "fe092cf3-2a9d-4556-a10a-56164ac52c3c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(-13+0j)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.matmul([2j, 3j], [2j, 3j])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "d4b905db-279b-43f5-bff8-04a86c1f7e4e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(-13+0j)"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x1 = np.array([2j, 3j])\n",
    "x2 = np.array([2j, 3j])\n",
    "x1 @ x2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1a7a61b-6c9c-463d-b522-51801422e818",
   "metadata": {},
   "source": [
    "## numpy.tensordot(a, b, axes=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "7f61d421-e0f9-4aa6-8013-e6a6f70ae788",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((5, 2),\n",
       " array([[4400., 4730.],\n",
       "        [4532., 4874.],\n",
       "        [4664., 5018.],\n",
       "        [4796., 5162.],\n",
       "        [4928., 5306.]]))"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(60.).reshape(3,4,5)\n",
    "b = np.arange(24.).reshape(4,3,2)\n",
    "c = np.tensordot(a,b, axes=([1,0],[0,1]))\n",
    "c.shape, c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "ef018f11-b8b3-40db-8835-96b317197b61",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ True,  True],\n",
       "       [ True,  True],\n",
       "       [ True,  True],\n",
       "       [ True,  True],\n",
       "       [ True,  True]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = np.zeros((5,2))\n",
    "for i in range(5):\n",
    "  for j in range(2):\n",
    "    for k in range(3):\n",
    "      for n in range(4):\n",
    "        d[i,j] += a[k,n,i] * b[n,k,j]\n",
    "c == d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "c2de84af-1a4f-4806-8ba9-265131fd195e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([['a', 'b'],\n",
       "       ['c', 'd']], dtype=object)"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array(range(1, 9))\n",
    "a.shape = (2, 2, 2)\n",
    "A = np.array(('a', 'b', 'c', 'd'), dtype=object)\n",
    "A.shape = (2, 2)\n",
    "a; A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "b9fc3bac-c591-48e9-95fe-7198bc2beedc",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['abbcccdddd', 'aaaaabbbbbbcccccccdddddddd'], dtype=object)"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.tensordot(a, A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "da1e7d7d-181a-4d5e-9c64-67a4b68d0832",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[['acc', 'bdd'],\n",
       "        ['aaacccc', 'bbbdddd']],\n",
       "\n",
       "       [['aaaaacccccc', 'bbbbbdddddd'],\n",
       "        ['aaaaaaacccccccc', 'bbbbbbbdddddddd']]], dtype=object)"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.tensordot(a, A, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "249629a5-d107-4d04-96c5-e99e81339540",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[[['a', 'b'],\n",
       "          ['c', 'd']],\n",
       "\n",
       "         [['aa', 'bb'],\n",
       "          ['cc', 'dd']]],\n",
       "\n",
       "\n",
       "        [[['aaa', 'bbb'],\n",
       "          ['ccc', 'ddd']],\n",
       "\n",
       "         [['aaaa', 'bbbb'],\n",
       "          ['cccc', 'dddd']]]],\n",
       "\n",
       "\n",
       "\n",
       "       [[[['aaaaa', 'bbbbb'],\n",
       "          ['ccccc', 'ddddd']],\n",
       "\n",
       "         [['aaaaaa', 'bbbbbb'],\n",
       "          ['cccccc', 'dddddd']]],\n",
       "\n",
       "\n",
       "        [[['aaaaaaa', 'bbbbbbb'],\n",
       "          ['ccccccc', 'ddddddd']],\n",
       "\n",
       "         [['aaaaaaaa', 'bbbbbbbb'],\n",
       "          ['cccccccc', 'dddddddd']]]]], dtype=object)"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.tensordot(a, A, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "aff594ef-19f1-4d25-955c-db2b2eff3778",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[['abbbbb', 'cddddd'],\n",
       "        ['aabbbbbb', 'ccdddddd']],\n",
       "\n",
       "       [['aaabbbbbbb', 'cccddddddd'],\n",
       "        ['aaaabbbbbbbb', 'ccccdddddddd']]], dtype=object)"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.tensordot(a, A, (0, 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "35280c06-1072-4494-aee7-9506c2d94e1c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[['abb', 'cdd'],\n",
       "        ['aaabbbb', 'cccdddd']],\n",
       "\n",
       "       [['aaaaabbbbbb', 'cccccdddddd'],\n",
       "        ['aaaaaaabbbbbbbb', 'cccccccdddddddd']]], dtype=object)"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.tensordot(a, A, (2, 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "496739e1-fa9a-40de-b498-7b08a602540c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['abbbcccccddddddd', 'aabbbbccccccdddddddd'], dtype=object)"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.tensordot(a, A, ((0, 1), (0, 1)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "afb2d701-4de7-4b4a-a362-77c4c8a8edfb",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['acccbbdddd', 'aaaaacccccccbbbbbbdddddddd'], dtype=object)"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.tensordot(a, A, ((2, 1), (1, 0)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1fb9609-fe9b-41b4-9bb7-8fdfc0219566",
   "metadata": {},
   "source": [
    "## numpy.einsum(subscripts, *operands, out=None, dtype=None, order='K', casting='safe', optimize=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "05d2e62e-358d-469c-87e4-f389d569624d",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "a = np.arange(25).reshape(5,5)\n",
    "b = np.arange(5)\n",
    "c = np.arange(6).reshape(2,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "7f4ca808-acaf-4b48-8231-5a34fab456f0",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "60"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.einsum('ii', a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "1e9f84c8-e397-4169-b925-26c7dfe2c14c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "60"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.einsum(a, [0,0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "de7c37a4-bb03-487f-8980-6c633d4b9e9a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "60"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.trace(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "b060344a-e43e-4324-87e8-dcb6c8249932",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  6, 12, 18, 24])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.einsum('ii->i', a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "8d2a3ef3-fb21-4dbc-b0ab-4f1b697ab1a6",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  6, 12, 18, 24])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.einsum(a, [0,0], [0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "3725f617-4cd8-4ffb-beb3-48908c311bc2",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  6, 12, 18, 24])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.diag(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "18e9c289-309b-40ef-9d8a-4e2d42759c0b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 10,  35,  60,  85, 110])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.einsum('ij->i', a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "438f165a-789a-4855-9714-e02e4353cb33",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 10,  35,  60,  85, 110])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.einsum(a, [0,1], [0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "aa7890d4-bbc4-4a63-ace7-5bf57efdf50c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 10,  35,  60,  85, 110])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(a, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "04072a95-dd15-4e84-be71-df277264d38d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 10,  35,  60,  85, 110])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.einsum('...j->...', a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "ae3e6b6b-f151-4b14-acd0-716aa92359b6",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 10,  35,  60,  85, 110])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.einsum(a, [Ellipsis,1], [Ellipsis])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "07aa33a8-8e4d-4ac4-b57a-7d6e74bf6b3e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 3],\n",
       "       [1, 4],\n",
       "       [2, 5]])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.einsum('ji', c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "3a684d38-a719-489f-a974-7ba6d8332a2c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 3],\n",
       "       [1, 4],\n",
       "       [2, 5]])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.einsum('ij->ji', c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "752a7818-9829-476c-a981-a7c1d578de8c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 3],\n",
       "       [1, 4],\n",
       "       [2, 5]])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.einsum(c, [1,0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "03b048cf-eeb2-48f8-ae50-fb86d5cd00ab",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 3],\n",
       "       [1, 4],\n",
       "       [2, 5]])"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.transpose(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "356f395d-94bb-499c-afa0-379052882b6f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "30"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.einsum('i,i', b, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "c6453f07-73a5-4d21-8a6a-6aa2d740cd87",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "30"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.einsum(b, [0], b, [0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "d2d99307-d433-4046-bfc2-ced53d185fda",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "30"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.inner(b,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "8d235f8f-8784-4b96-ae9d-8a35648d600b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 30,  80, 130, 180, 230])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.einsum('ij,j', a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "a7d8001b-b27b-4168-a11d-392dfc267c81",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 30,  80, 130, 180, 230])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.einsum(a, [0,1], b, [1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "a66d680d-94c9-4fb2-95d1-002c6c352eda",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 30,  80, 130, 180, 230])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "f8f47744-0ebc-4bd1-b73e-cfa82f637c29",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 30,  80, 130, 180, 230])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.einsum('...j,j', a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "0148fe38-1ad2-4e26-81ea-4d78ebe0e6f1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  3,  6],\n",
       "       [ 9, 12, 15]])"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.einsum('..., ...', 3, c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "dc9da507-801d-45bb-8dfc-23ed77edba1a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  3,  6],\n",
       "       [ 9, 12, 15]])"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.einsum(',ij', 3, c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "aaeaacf3-16b7-4c13-afaa-ad319bc4cdbb",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  3,  6],\n",
       "       [ 9, 12, 15]])"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.einsum(3, [Ellipsis], c, [Ellipsis])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "0ecdcf02-bef9-4852-95e4-04372769ca70",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  3,  6],\n",
       "       [ 9, 12, 15]])"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.multiply(3, c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "12b493ba-c261-4cfd-b28c-7b7b920323a5",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [0, 2, 4, 6, 8]])"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.einsum('i,j', np.arange(2)+1, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "08adc951-513b-4ae6-bda8-7294382ad8e4",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [0, 2, 4, 6, 8]])"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.einsum(np.arange(2)+1, [0], b, [1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "fe6b174e-23e5-4ac9-8a8f-3b433d5e9c55",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [0, 2, 4, 6, 8]])"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.outer(np.arange(2)+1, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "11fda4fa-8635-4c6d-8748-5d31ce8fa589",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4400., 4730.],\n",
       "       [4532., 4874.],\n",
       "       [4664., 5018.],\n",
       "       [4796., 5162.],\n",
       "       [4928., 5306.]])"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(60.).reshape(3,4,5)\n",
    "b = np.arange(24.).reshape(4,3,2)\n",
    "np.einsum('ijk,jil->kl', a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "d92776b8-2530-4436-93c7-7ad5235b7119",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4400., 4730.],\n",
       "       [4532., 4874.],\n",
       "       [4664., 5018.],\n",
       "       [4796., 5162.],\n",
       "       [4928., 5306.]])"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.einsum(a, [0,1,2], b, [1,0,3], [2,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "b8ce042f-a1ee-484b-9dbb-235245ed221f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4400., 4730.],\n",
       "       [4532., 4874.],\n",
       "       [4664., 5018.],\n",
       "       [4796., 5162.],\n",
       "       [4928., 5306.]])"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.tensordot(a,b, axes=([1,0],[0,1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "3d114bce-30cd-4d59-bdf2-827565ed063e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0.],\n",
       "       [0., 1., 0.],\n",
       "       [0., 0., 1.]])"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.zeros((3, 3))\n",
    "np.einsum('ii->i', a)[:] = 1\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "f72851c9-7370-4914-bcb4-8cd7f438b47a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[10, 28, 46, 64],\n",
       "       [13, 40, 67, 94]])"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(6).reshape((3,2))\n",
    "b = np.arange(12).reshape((4,3))\n",
    "np.einsum('ki,jk->ij', a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "0a3dcbd3-ec53-42a3-810a-cf39094aac87",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[10, 28, 46, 64],\n",
       "       [13, 40, 67, 94]])"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.einsum('ki,...k->i...', a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "86976335-5608-4ed8-9751-2f93867d1198",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[10, 28, 46, 64],\n",
       "       [13, 40, 67, 94]])"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.einsum('k...,jk', a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "223f2c6e-fac1-40b4-8de7-618d9653e8a0",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "a = np.ones(64).reshape(2,4,8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "8eb1f438-8f67-4116-b1a7-4f5243e955c6",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "for iteration in range(500):\n",
    "    _ = np.einsum('ijk,ilm,njm,nlk,abc->',a,a,a,a,a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "aa2740a9-83b5-46fa-842c-428a8414148b",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "for iteration in range(500):\n",
    "    _ = np.einsum('ijk,ilm,njm,nlk,abc->',a,a,a,a,a,\n",
    "        optimize='optimal')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "1fae7883-18c4-49c9-ab88-04697dd38dae",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "for iteration in range(500):\n",
    "    _ = np.einsum('ijk,ilm,njm,nlk,abc->',a,a,a,a,a, optimize='greedy')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "e656eadf-a28c-41b6-b8f1-3bd1c23d3a2e",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "path = np.einsum_path('ijk,ilm,njm,nlk,abc->',a,a,a,a,a, \n",
    "    optimize='optimal')[0]\n",
    "for iteration in range(500):\n",
    "    _ = np.einsum('ijk,ilm,njm,nlk,abc->',a,a,a,a,a, optimize=path)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5dd15494-7821-4292-8155-8ac003226b90",
   "metadata": {},
   "source": [
    "## numpy.einsum_path(subscripts, *operands, optimize='greedy')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "2ca1e485-14a8-492f-b7cd-bb92fbb0e5b1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['einsum_path', (1, 2), (0, 1)]\n"
     ]
    }
   ],
   "source": [
    "np.random.seed(123)\n",
    "a = np.random.rand(2, 2)\n",
    "b = np.random.rand(2, 5)\n",
    "c = np.random.rand(5, 2)\n",
    "path_info = np.einsum_path('ij,jk,kl->il', a, b, c, optimize='greedy')\n",
    "print(path_info[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "2a500ffb-9ec8-4b0c-a49a-56c20ac3e6fe",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Complete contraction:  ij,jk,kl->il\n",
      "         Naive scaling:  4\n",
      "     Optimized scaling:  3\n",
      "      Naive FLOP count:  1.200e+02\n",
      "  Optimized FLOP count:  5.700e+01\n",
      "   Theoretical speedup:  2.105\n",
      "  Largest intermediate:  4.000e+00 elements\n",
      "--------------------------------------------------------------------------\n",
      "scaling                  current                                remaining\n",
      "--------------------------------------------------------------------------\n",
      "   3                   kl,jk->jl                                ij,jl->il\n",
      "   3                   jl,ij->il                                   il->il\n"
     ]
    }
   ],
   "source": [
    "print(path_info[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "e64cd72a-081b-4e6d-9b32-43de2be7db3b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['einsum_path', (0, 2), (0, 3), (0, 2), (0, 1)]\n"
     ]
    }
   ],
   "source": [
    "I = np.random.rand(10, 10, 10, 10)\n",
    "C = np.random.rand(10, 10)\n",
    "path_info = np.einsum_path('ea,fb,abcd,gc,hd->efgh', C, C, I, C, C,\n",
    "                           optimize='greedy')\n",
    "print(path_info[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "3907d6bc-08d1-4003-9d4e-ede70c47dd29",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Complete contraction:  ea,fb,abcd,gc,hd->efgh\n",
      "         Naive scaling:  8\n",
      "     Optimized scaling:  5\n",
      "      Naive FLOP count:  5.000e+08\n",
      "  Optimized FLOP count:  8.000e+05\n",
      "   Theoretical speedup:  624.999\n",
      "  Largest intermediate:  1.000e+04 elements\n",
      "--------------------------------------------------------------------------\n",
      "scaling                  current                                remaining\n",
      "--------------------------------------------------------------------------\n",
      "   5               abcd,ea->bcde                      fb,gc,hd,bcde->efgh\n",
      "   5               bcde,fb->cdef                         gc,hd,cdef->efgh\n",
      "   5               cdef,gc->defg                            hd,defg->efgh\n",
      "   5               defg,hd->efgh                               efgh->efgh\n"
     ]
    }
   ],
   "source": [
    "print(path_info[1]) "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf1f8f6f-48cf-4157-9234-0489e7b6fba8",
   "metadata": {},
   "source": [
    "## linalg.matrix_power(a, n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "fedd8699-9ba2-497f-92f0-b79372102f92",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0, -1],\n",
       "       [ 1,  0]])"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from numpy.linalg import matrix_power\n",
    "i = np.array([[0, 1], [-1, 0]])\n",
    "matrix_power(i, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "c1a5fef4-8006-46ee-bffb-d9e42b966029",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 0],\n",
       "       [0, 1]])"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matrix_power(i, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "a5eb176b-f83f-4e92-9ffd-7e6bfe6caf12",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.,  1.],\n",
       "       [-1.,  0.]])"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matrix_power(i, -3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "fb87831c-8be9-4a59-bcbc-e778700bc070",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0., -1.,  0.,  0.],\n",
       "       [ 1.,  0.,  0.,  0.],\n",
       "       [ 0.,  0.,  0.,  1.],\n",
       "       [ 0.,  0., -1.,  0.]])"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "q = np.zeros((4, 4))\n",
    "q[0:2, 0:2] = -i\n",
    "q[2:4, 2:4] = i\n",
    "q"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "87ed2459-7639-45c4-b62a-ace22884834c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.,  0.,  0.,  0.],\n",
       "       [ 0., -1.,  0.,  0.],\n",
       "       [ 0.,  0., -1.,  0.],\n",
       "       [ 0.,  0.,  0., -1.]])"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matrix_power(q, 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "379c27cf-bab7-4cf9-8dd3-f1256e8d967d",
   "metadata": {},
   "source": [
    "## numpy.kron(a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "da64a29d-21fd-4b28-b632-164bf8ca35cd",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  5,   6,   7,  50,  60,  70, 500, 600, 700])"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.kron([1,10,100], [5,6,7])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "4abf4e13-0f2d-46a9-97c3-e20dd3792a57",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  5,  50, 500,   6,  60, 600,   7,  70, 700])"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.kron([5,6,7], [1,10,100])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "317a2796-c4fb-4bd5-9631-1d1b05ff26b1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [0., 0., 1., 1.],\n",
       "       [0., 0., 1., 1.]])"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.kron(np.eye(2), np.ones((2,2)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "ee8088e9-12cb-45cc-a31a-ebe10274a3cd",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 10, 6, 20)"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(100).reshape((2,5,2,5))\n",
    "b = np.arange(24).reshape((2,3,4))\n",
    "c = np.kron(a,b)\n",
    "c.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "5759fd05-8fff-462d-a99a-875d294cea5d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "I = (1,3,0,2)\n",
    "J = (0,2,1)\n",
    "J1 = (0,) + J             # extend to ndim=4\n",
    "S1 = (1,) + b.shape\n",
    "K = tuple(np.array(I) * np.array(S1) + np.array(J1))\n",
    "c[K] == a[I]*b[J]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9f49ac7-6a93-4b73-9ee4-20e50e886135",
   "metadata": {},
   "source": [
    "# 分解\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "linalg.cholesky(a)|Cholesky分解\n",
    "linalg.qr(a[, mode])|计算矩阵的QR分解。\n",
    "linalg.svd(a[, full_matrices, compute_uv, …])|奇异值分解"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e75c4724-2a53-483c-bf79-f948df60a949",
   "metadata": {},
   "source": [
    "## linalg.cholesky(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "02aeae4b-1f5d-4229-867f-e835251b49e3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.+0.j, -0.-2.j],\n",
       "       [ 0.+2.j,  5.+0.j]])"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.array([[1,-2j],[2j,5]])\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "b2015b4c-2f01-4379-ba80-3900bedfa8c4",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.+0.j, 0.+0.j],\n",
       "       [0.+2.j, 1.+0.j]])"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L = np.linalg.cholesky(A)\n",
    "L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "7d8bf429-30bc-4f90-a18b-e6c5acecd86b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.+0.j, 0.-2.j],\n",
       "       [0.+2.j, 5.+0.j]])"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(L, L.T.conj())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "226b3bd7-4dc4-45ba-b8cc-3c4c74b93c53",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.+0.j, 0.+0.j],\n",
       "       [0.+2.j, 1.+0.j]])"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = [[1,-2j],[2j,5]]\n",
    "np.linalg.cholesky(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "1492a686-4f54-4250-aae6-25c1095b8804",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1.+0.j, 0.+0.j],\n",
       "        [0.+2.j, 1.+0.j]])"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linalg.cholesky(np.matrix(A))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66e3a4d6-cf21-4f17-a36a-4e60a26f3f10",
   "metadata": {
    "tags": []
   },
   "source": [
    "## linalg.qr(a, mode='reduced')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "6ab7d39c-39ab-4e6f-ada0-d617276ea1d7",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randn(9, 6)\n",
    "Q, R = np.linalg.qr(a)\n",
    "np.allclose(a, np.dot(Q, R))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "30327da0-8e1a-434f-9d48-dfc6bd7d1dc7",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "R2 = np.linalg.qr(a, mode='r')\n",
    "np.allclose(R, R2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "eb4cd55c-fec8-4f27-b2de-312106ace5f3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1],\n",
       "       [1, 1],\n",
       "       [1, 1],\n",
       "       [2, 1]])"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.array([[0, 1], [1, 1], [1, 1], [2, 1]])\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "f51ea9aa-78ad-4dc7-a5fd-ed573f5e9f1e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 1.])"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.array([1, 2, 2, 3])\n",
    "Q, R = np.linalg.qr(A)\n",
    "p = np.dot(Q.T, b)\n",
    "np.dot(np.linalg.inv(R), p)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb5e0b6a-cc6b-4325-b73e-ed3e216f75bb",
   "metadata": {},
   "source": [
    "## linalg.svd(a, full_matrices=True, compute_uv=True, hermitian=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "4f242ae1-d0d9-4106-b389-a710fd81a250",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((9, 6), (6,), (6, 6))"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "U, S, Vh = np.linalg.svd(a, full_matrices=False)\n",
    "U.shape, S.shape, Vh.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc3be8e9-a1e1-4588-9202-8354c7584dbf",
   "metadata": {},
   "source": [
    "# 矩阵特征值\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "linalg.eig(a)|计算方阵的特征值和右特征向量。\n",
    "linalg.eigh(a[, UPLO])|返回复数Hermitian（共轭对称）或实对称矩阵的特征值和特征向量。\n",
    "linalg.eigvals(a)|计算通用矩阵的特征值。\n",
    "linalg.eigvalsh(a[, UPLO])|计算复杂的Hermitian或实对称矩阵的特征值。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "070ab342-d626-43eb-8064-ef818b24bba9",
   "metadata": {},
   "source": [
    "## linalg.eig(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "3a2f4c99-361c-4098-bb34-023b984c9b35",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from numpy import linalg as LA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "be1ebcd9-a29c-4bad-8165-a7cf5f16e1af",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([1., 2., 3.]),\n",
       " array([[1., 0., 0.],\n",
       "        [0., 1., 0.],\n",
       "        [0., 0., 1.]]))"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eigenvalues, eigenvectors = LA.eig(np.diag((1, 2, 3)))\n",
    "eigenvalues, eigenvectors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "5c88d46f-ec8f-44dc-9154-7a8c4ec5b029",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([1.+1.j, 1.-1.j]),\n",
       " array([[0.70710678+0.j        , 0.70710678-0.j        ],\n",
       "        [0.        -0.70710678j, 0.        +0.70710678j]]))"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eigenvalues, eigenvectors = LA.eig(np.array([[1, -1], [1, 1]]))\n",
    "eigenvalues, eigenvectors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "74de3b13-1041-439d-81b3-774c919e6141",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([2.+0.j, 0.+0.j]),\n",
       " array([[ 0.        +0.70710678j,  0.70710678+0.j        ],\n",
       "        [ 0.70710678+0.j        , -0.        +0.70710678j]]))"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1, 1j], [-1j, 1]])\n",
    "eigenvalues, eigenvectors = LA.eig(a)\n",
    "eigenvalues, eigenvectors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "85361ac2-a0f5-49bb-8015-1dce8be539ad",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([1., 1.]),\n",
       " array([[1., 0.],\n",
       "        [0., 1.]]))"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1 + 1e-9, 0], [0, 1 - 1e-9]])\n",
    "eigenvalues, eigenvectors = LA.eig(a)\n",
    "eigenvalues, eigenvectors"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d84da58-2870-498c-9e94-42aac66d8c52",
   "metadata": {
    "tags": []
   },
   "source": [
    "## linalg.eigh(a, UPLO='L')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "eceb3767-4d9e-48b4-93ba-a96bd64d685b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.+0.j, -0.-2.j],\n",
       "       [ 0.+2.j,  5.+0.j]])"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1, -2j], [2j, 5]])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "df835a6e-94d7-49c7-995d-5b41eb2e1caa",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([0.17157288, 5.82842712]),\n",
       " array([[-0.92387953+0.j        , -0.38268343+0.j        ],\n",
       "        [ 0.        +0.38268343j,  0.        -0.92387953j]]))"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eigenvalues, eigenvectors = LA.eigh(a)\n",
    "eigenvalues, eigenvectors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "e813ac94-9894-40a6-9c3c-8b17ee6c03f9",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5.55111512e-17+0.0000000e+00j, 0.00000000e+00+1.2490009e-16j])"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(np.dot(a, eigenvectors[:, 0]) - \n",
    "eigenvalues[0] * eigenvectors[:, 0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "2af8bc5e-a5ae-48dc-9264-25bdfea86b8b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.+0.j, 0.+0.j])"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(np.dot(a, eigenvectors[:, 1]) - \n",
    "eigenvalues[1] * eigenvectors[:, 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "9b37a5e6-b53c-4714-943c-ce1a5763577d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 1.+0.j, -0.-2.j],\n",
       "        [ 0.+2.j,  5.+0.j]])"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.matrix(a)\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "9a55edeb-1a72-4092-b095-f64d59551933",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([0.17157288, 5.82842712]),\n",
       " matrix([[-0.92387953+0.j        , -0.38268343+0.j        ],\n",
       "         [ 0.        +0.38268343j,  0.        -0.92387953j]]))"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eigenvalues, eigenvectors = LA.eigh(A)\n",
    "eigenvalues, eigenvectors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "id": "6ead5bd5-6da4-4a7b-9fc7-c610137ae7dc",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5.+2.j, 9.-2.j],\n",
       "       [0.+2.j, 2.-1.j]])"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[5+2j, 9-2j], [0+2j, 2-1j]])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "631760ba-f9e3-4a6d-9037-9515cced896b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5.+0.j, 0.-2.j],\n",
       "       [0.+2.j, 2.+0.j]])"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.array([[5.+0.j, 0.-2.j], [0.+2.j, 2.-0.j]])\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "ffaa9b06-a4ac-4725-accb-537696d023c7",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([6.+0.j, 1.+0.j])"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wa, va = LA.eigh(a)\n",
    "wb, vb = LA.eig(b)\n",
    "wa; wb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "43821130-a83c-439e-959a-20c5f6b646bf",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.89442719+0.j       , -0.        +0.4472136j],\n",
       "       [-0.        +0.4472136j,  0.89442719+0.j       ]])"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "va; vb"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6ae02e2-e74f-4b57-aaaa-7f83f692bda5",
   "metadata": {
    "tags": []
   },
   "source": [
    "## np.linalg.eigvals(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "id": "bd5b154f-ee56-4ace-9b87-4c6eb9665256",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1.0, 1.0, 0.0)"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.random.random()\n",
    "Q = np.array([[np.cos(x), -np.sin(x)], [np.sin(x), np.cos(x)]])\n",
    "LA.norm(Q[0, :]), LA.norm(Q[1, :]), np.dot(Q[0, :],Q[1, :])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "6b657760-55b6-4afa-973f-493cb3cabd0f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1.,  1.])"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "D = np.diag((-1,1))\n",
    "LA.eigvals(D)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "fc1cf3fd-5ce7-484f-ad29-556604e9b4ca",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1.,  1.])"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.dot(Q, D)\n",
    "A = np.dot(A, Q.T)\n",
    "LA.eigvals(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b23301d-963e-4c53-b04e-7103c6ba35b4",
   "metadata": {
    "tags": []
   },
   "source": [
    "## linalg.eigvalsh(a, UPLO='L')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "b9cf2b67-41c0-4bd8-87b3-02817fb8328e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.17157288, 5.82842712])"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1, -2j], [2j, 5]])\n",
    "LA.eigvalsh(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "1a8e78ec-ce44-49be-bd76-298d28ce27cc",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5.+2.j, 9.-2.j],\n",
       "       [0.+2.j, 2.-1.j]])"
      ]
     },
     "execution_count": 124,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[5+2j, 9-2j], [0+2j, 2-1j]])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "7f7a1f24-217b-423e-9bf7-a3a1d8dc82de",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5.+0.j, 0.-2.j],\n",
       "       [0.+2.j, 2.+0.j]])"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.array([[5.+0.j, 0.-2.j], [0.+2.j, 2.-0.j]])\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "id": "73cddb71-8f4c-401d-a17b-1271eac1dd90",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([6.+0.j, 1.+0.j])"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wa = LA.eigvalsh(a)\n",
    "wb = LA.eigvals(b)\n",
    "wa; wb"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "97c03007-594f-4ffe-93f8-ce94b682e826",
   "metadata": {},
   "source": [
    "# 范数和其他数字\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "linalg.norm(x[, ord, axis, keepdims])|矩阵或向量范数。\n",
    "linalg.cond(x[, p])|计算矩阵的条件数。\n",
    "linalg.det(a)|计算数组的行列式。\n",
    "linalg.matrix_rank(M[, tol, hermitian])|使用SVD方法返回数组的矩阵的rank\n",
    "linalg.slogdet(a)|计算数组行列式的符号和（自然）对数。\n",
    "trace(a[, offset, axis1, axis2, dtype, out])|返回数组对角线的和。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77225cdf-350a-4110-8eae-fd0e10fa0bf1",
   "metadata": {},
   "source": [
    "## linalg.norm(x, ord=None, axis=None, keepdims=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "id": "6401bfc3-7cb9-4b41-bb37-363ecf60a25a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-4, -3, -2, -1,  0,  1,  2,  3,  4])"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(9) - 4\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "47eb62fc-9adc-41e6-be76-fd71553fbd84",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-4, -3, -2],\n",
       "       [-1,  0,  1],\n",
       "       [ 2,  3,  4]])"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = a.reshape((3, 3))\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "id": "876a0732-db98-4a2a-9d12-d0af5550b8e6",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7.745966692414834"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LA.norm(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "id": "50788d68-76d8-4370-84d1-dfd408a4b50b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7.745966692414834"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LA.norm(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "id": "d1b3b04f-e4e0-44d9-9d5c-ab3e44f1c3fc",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7.745966692414834"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LA.norm(b, 'fro')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "id": "1eef9a8a-9430-4781-b7f6-4d16e7586c11",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.0"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LA.norm(a, np.inf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "id": "8f4d85ef-4c4b-478a-a0e9-351ffe956b4d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9.0"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LA.norm(b, np.inf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "id": "712bf8d4-4756-4e14-929f-654f6ee6a7f9",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LA.norm(a, -np.inf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "774cc7c2-e7a0-4c18-9ca8-8dbbb7c6887e",
   "metadata": {},
   "outputs": [],
   "source": [
    "LA.norm(b, -np.inf)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc-autonumbering": true
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
